<?php
/*
    Author: dkloz
    purpose: creates a category in the name of user with that Id. It adds a category
    and then one by one its attributes
    returns BL_OK upon success else code of failure
    
*/
function BL_createCategory($category, $userId)
{
    $owner = BL_getUserData($userId);


	/*
    //Check if category has same name
    $public = array();
    $public = DB_getPublicCategories();


	//Check if a category with the same name exists...
    foreach($public as $cat){
		if(!strcmp($cat->getName(), $category->GetName() ) ) {
			return BL_SAME_NAME_CAT;
		}
    }
    
	$private = array();
	$private = DB_getPrivateCategories();
	
	foreach($private as $cat){
		if(!strcmp($cat->getName(), $category->GetName() ) ) {
			return BL_SAME_NAME_CAT;
		}
    }


	*/
    //if not then add it 
    $error = DB_addCategory($category, $userId);

    if ($error < 0) {
    		UI_registerError('msg_error', "Category Name Exists");
        return BL_ADD_CATEGORY;
    }

    $categoryId = $error;
    $attrs = $category->GetAttributes();
    $count = count($attrs);
	//echo "cat id is " . $categoryId .  "Before <br>";
	//print_r($attrs);

	//echo "<br> after <br>";
    for ($i = 0; $i < $count; $i++) {
    
    		$attrs[$i]->SetCategoryId($categoryId);
    		//print_r($attrs);
        $error = DB_addAttributeToCategory($attrs[$i], $categoryId);

        if ($error != DB_OK) {
        	//	echo "DB add attribute to category returned error code " . $error . "<br>";
            return BL_ADD_ATTRIBUTE_TO_CATEGORY;
        }
    }
    return BL_enableCategory($categoryId, $userId);
}
?>
